<?php

namespace app\model;

use think\Model;

class AdminMenu extends Model
{
    public $id;
    public $menuId;
    public $menuName;
    public $menuIcon;
    public $menuUrl;
    public $topId;

    private $data;

    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->data = array();
    }

    public function __destruct()
    {
        parent::__destruct();
        unset(
            $this->data
        );
    }

    private function end()
    {
        return set_menu_cache(get_admin_role_menu(get_admin_user_id()));
    }

    public function addTopMenu()
    {
        $this->data['menu_name'] = $this->menuName;
        $this->data['menu_icon'] = $this->menuIcon;
        $this->data['top_state'] = 1;
        $this->data['top_id'] = 0;
        $this->save($this->data);
        return $this->end();
    }

    public function addSonMenu()
    {
        $this->data['top_id'] = $this->topId;
        $this->data['menu_name'] = $this->menuName;
        $this->data['menu_url'] = $this->menuUrl;
        $this->save($this->data);
        return $this->end();
    }

    public function deleteMenu()
    {
        $this->data['delete_state'] = 1;
        $this->data['delete_time'] = time();
        $this->where('id', $this->id)->save($this->data);
        return $this->end();
    }

    public function read()
    {
        return $this->where('disable_state', 0)
            ->where('delete_state', 0)
            ->where('top_state', 1)
            ->field('id, menu_name, menu_icon')
            ->select()->each(
                function ($item){
                    $item['child'] = $this->where('top_id', $item['id'])
                        ->where('disable_state', 0)
                        ->where('delete_state', 0)
                        ->field('menu_name, menu_url')
                        ->select()->toArray();
                }
            )->toArray();
    }

    public function queryMenu()
    {
        $menuList = explode('|', $this->menuId);
        foreach ($menuList as $value)
        {
            $this->data[] = $this->where('id', $value)
                ->where('disable_state', 0)
                ->where('delete_state', 0)
                ->field('id, menu_name')
                ->find()->toArray();
        }
        return $this->data;
    }

    public function getSignInUsersMenuData()
    {
        $menuList = explode('|', $this->menuId);
        foreach ($menuList as $value)
        {
            $menuData =  $this->where('id', $value)
                ->where('disable_state', 0)
                ->where('delete_state', 0)
                ->where('top_state', 1)
                ->field('id, menu_name, menu_icon')
                ->find()->toArray();
            $menuData['child'] = $this->where('top_id', $menuData['id'])
                ->where('disable_state', 0)
                ->where('delete_state', 0)
                ->field('menu_name, menu_url')
                ->select()->toArray();
            $this->data[] = $menuData;
        }
        return $this->data;
    }

    public function getAllMenuData()
    {
        return $this->field('id, menu_name, menu_url, menu_icon, top_id, top_state')
            ->where('delete_state', 0)
            ->select()->toArray();
    }

    public function queryTop()
    {
        return $this->where('disable_state', 0)
            ->where('delete_state', 0)
            ->where('top_state', 1)
            ->field('id, menu_name')->select()->toArray();
    }

    public function queryMenuData()
    {
        return $this->where('id', $this->id)
            ->field('id, menu_name, menu_url, menu_icon, top_id')
            ->find()->toArray();
    }

    public function editMenuData()
    {
        $this->data['menu_name'] = $this->menuName;
        $this->data['menu_icon'] = $this->menuIcon;
        $this->data['menu_url'] = $this->menuUrl;
        $this->where('id', $this->id)
            ->save($this->data);
        return $this->end();
    }
}